#include "mainwidget.h"
#include "./ui_mainwidget.h"

mainWidget* mainWidget::instance = nullptr;
mainWidget* mainWidget:: mainWidgetInstance()
{
    if(instance == nullptr)
    {
        //此处不指定父窗口，默认使用桌面为主窗口
        instance = new mainWidget();
    }
    return instance;
}

//初始化主窗口
void mainWidget::initMainWidget()
{
    //使用水平布局管理器
    QHBoxLayout* layout = new QHBoxLayout();
    this->setLayout(layout);

    //设置布局管理器中的元素的分割间隔
    layout->setSpacing(0);
    //设置布局管理器中的元素距离主窗口上下左右间距
    layout->setContentsMargins(0,0,0,0);
    //实例化三个部分的窗口
    this->mainWindowLeft = new QWidget();
    this->mainWindowMid = new QWidget();
    this->mainWindowRight = new QWidget();
    //设置窗口尺寸
    this->mainWindowLeft->setFixedWidth(70);
    this->mainWindowMid->setFixedWidth(260);
    //为什么设置为最小？因为要支持动态大小
    this->mainWindowRight->setMinimumWidth(650);

    //使用QSS区分三个窗口的颜色
    this->mainWindowLeft->setStyleSheet("QWidget { background-color: rgb(46,46,46); }");
    this->mainWindowMid->setStyleSheet("QWidget { background-color: rgb(247,247,247);}");
    this->mainWindowRight->setStyleSheet("QWidget {background-color: rgb(245,245,245); }");
    //添加进布局管理器
    layout->addWidget(mainWindowLeft);
    layout->addWidget(mainWindowMid);
    layout->addWidget(mainWindowRight);
}

void mainWidget::initLeftWidget()
{
    //使用垂直布局管理器
    QVBoxLayout* layout = new QVBoxLayout();
    //一个QWidget中默认智能有一个布局管理器
    mainWindowLeft->setLayout(layout);
    //设置布局管理器内元素的间隔
    layout->setSpacing(20);
    layout->setContentsMargins(0,30,0,0);
    userAvaterButton = new QPushButton();
    //设置头像尺寸
    userAvaterButton->setFixedSize(45,45);
    userAvaterButton->setIconSize(QSize(45,45));
    userAvaterButton->setIcon(QIcon(":/resource/image/avater.png"));
    layout->addWidget(userAvaterButton,1,Qt::AlignTop | Qt::AlignHCenter);

    //设置会话按钮
    sessionListButtton = new QPushButton();
    sessionListButtton->setFixedSize(30,30);
    sessionListButtton->setIconSize(QSize(30,30));
    sessionListButtton->setIcon(QIcon(":/resource/image/session_active.png"));
    sessionListButtton->setStyleSheet("QPushButton { background-color: transparent; }");
    //禁用按钮点击反馈
    sessionListButtton->setFlat(true);
    layout->addWidget(sessionListButtton,1,Qt::AlignTop | Qt::AlignHCenter);
    //设置好友列表按钮
    friendListButton = new QPushButton();
    friendListButton->setFixedSize(30,30);
    friendListButton->setIconSize(QSize(30,30));
    friendListButton->setIcon(QIcon(":/resource/image/friend_inactive.png"));
    friendListButton->setStyleSheet("QPushButton { background-color: transparent; }");
    //禁用按钮点击反馈
    friendListButton->setFlat(true);
    layout->addWidget(friendListButton,1,Qt::AlignTop | Qt::AlignHCenter);
    //设置好友申请按钮
    applyButton = new QPushButton();
    applyButton->setFixedSize(30,30);
    applyButton->setIconSize(QSize(30,30));
    applyButton->setIcon(QIcon(":/resource/image/apply_inactive.png"));
    applyButton->setStyleSheet("QPushButton { background-color: transparent; }");
    //禁用按钮点击反馈
    applyButton->setFlat(true);
    layout->addWidget(applyButton,1,Qt::AlignTop | Qt::AlignHCenter);

    //增加空白区域将其他元素挤上去
    layout->addStretch(100);
}

//初始化中间窗口
void mainWidget::initMidWidget()
{
    QGridLayout* layout = new QGridLayout();
    layout->setSpacing(0);
    layout->setContentsMargins(0,20,0,0);
    mainWindowMid->setLayout(layout);
    //初始化搜索框
    searchEdit = new QLineEdit();
    //设置固定高度
    searchEdit->setFixedHeight(30);
    //设置搜索输入框样式
    QString searchStyle = "QLineEdit { border-radius: 5px; background-color:rgb(226,226,226);padding-left:5px;}";
    searchEdit->setStyleSheet(searchStyle);
    searchEdit->setPlaceholderText("搜索");
    //实例化添加好友按钮
    addFriendButton  = new QPushButton();
    addFriendButton->setFixedSize(30,30);
    //设置图标
    addFriendButton->setIcon(QIcon(":/resource/image/cross.png"));
    //设置按钮样式
    QString addBtnStyle = "QPushButton { border-radius: 5px; background-color:rgb(226,226,226);}";
    addBtnStyle += "QPushButton:pressed{ background-color:rgb(240,240,240); }";
    addFriendButton->setStyleSheet(addBtnStyle);
    //设置网格布局
    //为了更美观，使用空白窗口填充边距
    QWidget* space1 = new QWidget();
    space1->setFixedWidth(10);
    QWidget* space2 = new QWidget();
    space2->setFixedWidth(10);
    QWidget* space3 = new QWidget();
    space3->setFixedWidth(10);

    layout->addWidget(space1,0,0);
    layout->addWidget(searchEdit,0,1); // 0行1列
    layout->addWidget(space2,0,2);
    layout->addWidget(addFriendButton,0,3); // 0行3列
    layout->addWidget(space3,0,4);
    SessionFriendArea* sessionArea = new SessionFriendArea();
    //使用网格布局管理器时，只是添加行、列信息会默认和前面添加的元素对齐,所有要设置占位符大小
    layout->addWidget(sessionArea,1,0,1,5);
}

//初始化最右侧窗口
void mainWidget::initRightWidget()
{

}

//初始化leftWidget中按钮的点击事件
void mainWidget::initSignalSlot()
{
    connect(sessionListButtton,&QPushButton::clicked,this,&mainWidget::sessionButtonClicked);
    connect(friendListButton,&QPushButton::clicked,this,&mainWidget::friendListButtonClicked);
    connect(applyButton,&QPushButton::clicked,this,&mainWidget::applyButtonClicked);
}
//处理会话按钮的点击事件
void mainWidget::sessionButtonClicked()
{
    currentTabStatu = ACTIVE_TAB::SESSION_LIST;
    //切换图标状态
    sessionListButtton->setIcon(QIcon(":/resource/image/session_active.png"));
    friendListButton->setIcon(QIcon(":/resource/image/friend_inactive.png"));
    applyButton->setIcon(QIcon(":/resource/image/apply_inactive.png"));
    //加载会话列表
    this->loadSessionList();
}
//处理好友列表按钮的点击事件
void mainWidget::friendListButtonClicked()
{
    currentTabStatu = ACTIVE_TAB::FRIEND_LIST;
    //切换图标状态
    friendListButton->setIcon(QIcon(":/resource/image/friend_active.png"));
    sessionListButtton->setIcon(QIcon(":/resource/image/session_inactive.png"));
    applyButton->setIcon(QIcon(":/resource/image/apply_inactive.png"));
    //加载好友列表
    this->loadFriendList();
}
//处理好友申请按钮的点击事件
void mainWidget::applyButtonClicked()
{
    currentTabStatu = ACTIVE_TAB::APPLY_LIST;
    //切换图标状态
    applyButton->setIcon(QIcon(":/resource/image/apply_active.png"));
    sessionListButtton->setIcon(QIcon(":/resource/image/session_inactive.png"));
    friendListButton->setIcon(QIcon(":/resource/image/friend_inactive.png"));
    //加载好友申请列表
    this->loadApplyList();
}

//加载会话列表
void mainWidget::loadSessionList()
{
    //TODO
}

//加载好友列表
void mainWidget::loadFriendList()
{
    //TODO
}

//加载好友申请列表
void mainWidget::loadApplyList()
{
    //TODO
}

mainWidget::mainWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::mainWidget)
{
    ui->setupUi(this);
    this->setWindowTitle("微信");
    this->setWindowIcon(QIcon(":/resource/image/Logo.png"));

    /*初始化主窗口布局*/
    initMainWidget();
    //初始化最左侧窗口布局
    initLeftWidget();
    //初始化中间窗口布局
    initMidWidget();
    //初始化最右侧窗口布局
    initRightWidget();
    //初始化按钮事件槽函数
    initSignalSlot();
}

mainWidget::~mainWidget()
{
    delete ui;
}
